<?php

class Lock extends CApplicationComponent
{
    public $lockPool;
    
    public function init(){
        $this->lockPool = Yii::app()->dbRedis;
    }
	public function check($lockName){
		$lock = $this->lockPool->get($lockName);
		return $lock;
	}
    
    public function get($lockName,$timeOut = false){
        $lock = !$this->lockPool->setnx($lockName,time());
        if ($timeOut !== false && !$lock){
            $this->setTimeOut($lockName, $timeOut);
        }
        return $lock;
    }
    
    public function setTimeOut($lockName,$timeOut){
        $this->lockPool->expireAt($lockName,$timeOut);
        return $this;
    }
    
    public function del($lockName){
        return $this->lockPool->delete($lockName);
    }
}